package com.tongji.housediy.utils
{
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import mx.controls.Label;

	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class TextFieldPool
	{
		private static var MAX_VALUE:uint;
		private static var GROWTH_VALUE:uint;
		private static var counter:uint;
		private static var pool:Vector.<TextField>;
		private static var currentTextField:TextField;
		
		private static var textFormat:TextFormat = new TextFormat("宋体", 18, null, true);
		
		public static function initialize(maxPoolSize:uint, growthValue:uint):void
		{
			MAX_VALUE = maxPoolSize;
			GROWTH_VALUE = growthValue;
			counter = maxPoolSize;
			var i:uint = maxPoolSize;
			pool = new Vector.<TextField>(MAX_VALUE);
			while( --i > -1 )
				pool[i] = createNewTextField();
		}
		
		private static function createNewTextField():TextField
		{
			var textField:TextField = new TextField();
			textField.selectable = false;
			textField.autoSize = TextFieldAutoSize.CENTER;
			textField.defaultTextFormat = textFormat;
			
			return textField;
		}
		
		public static function getTextField():TextField
		{
			if ( counter > 0 )
				return currentTextField = pool[--counter];
			var i:uint = GROWTH_VALUE;
			while( --i > -1 )
				pool.unshift(createNewTextField());
			counter = GROWTH_VALUE;
			return getTextField();
		}
		
		public static function disposeTextField(disposedTextField:TextField):void
		{
			pool[counter++] = disposedTextField;
		}
	}
}